package com.safeneeds.linguimana.ser.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.safeneeds.linguimana.dto.DataStorePort;
import com.safeneeds.linguimana.dto.PortConstant;
import com.safeneeds.linguimana.entity.PortNews;
import com.safeneeds.linguimana.entity.PortNewsCate;
import com.safeneeds.linguimana.entity.PortNewsCateMap;
import com.safeneeds.linguimana.entity.PortNewsCateMapId;
import com.safeneeds.linguimana.entity.dao.IPortNewsCateDao;
import com.safeneeds.linguimana.entity.dao.IPortNewsCateMapDao;
import com.safeneeds.linguimana.entity.dao.IPortNewsDao;
import com.safeneeds.linguimana.ser.IBgNewsCateService;
import com.safeneeds.linguimana.ser.IBgNewsService;
import com.safeneeds.util.base.IUserInfo;
import com.safeneeds.util.base.RetType;
import com.safeneeds.util.other.StringUtil;
import com.safeneeds.util.page.PageInfo;
import com.safeneeds.util.page.ser.IPageService;

/**
 * 门户新闻管理
 * @author gaof
 * @date 2017年2月8日
 */
@Service
@Transactional
public class BgNewsService implements IBgNewsService {

	@Resource(name="bgNewsCateService")
	private IBgNewsCateService bgNewsCateService;
	
	@Resource(name="pageService")
	private IPageService pageService;
	
	@Resource(name="portNewsDao")
	private IPortNewsDao portNewsDao;
	
	@Resource(name="portNewsCateDao")
	private IPortNewsCateDao portNewsCateDao;
	
	@Resource(name="portNewsCateMapDao")
	private IPortNewsCateMapDao portNewsCateMapDao;
	
	/**
	 * 新闻列表
	 * @param pageInfo
	 * @param dataStore
	 * @return
	 */
	@Override
	public PageInfo listNewsPage(PageInfo pageInfo, DataStorePort dataStore) {
		
		List<PortNewsCate> cateList = null;
		
		//通过主分类获取其所有下级子分类，如果没有下级分类，则直接取该分类下的新闻
		if(!StringUtil.isEmpty(dataStore.getMainCateId())){
			
			//获取该分类信息
			PortNewsCate cate = portNewsCateDao.getNewsCateById(dataStore.getMainCateId());
			if(cate == null){
				return pageInfo;
			}
			cateList = new ArrayList<PortNewsCate>();
			cateList.add(cate);
			//获取所有子分类
			bgNewsCateService.getAllChildCate(cate, cateList);
		}
		
		//拼接子分类，用户查询
		if(cateList != null && !cateList.isEmpty()){
			String cates = "";
			//拼接
			for(PortNewsCate item : cateList){
				cates += "'" + item.getCateId() + "',";
			}
			//去掉最后一个“,”号
			cates = cates.substring(0, cates.lastIndexOf(","));
			dataStore.setCatesSplice(cates);
		}
		
		pageInfo = portNewsDao.listNewsPage(pageInfo, dataStore);
		pageInfo = pageService.getSQLPageInfo(pageInfo);
		
		return pageInfo;
	}

	/**
	 * 添加或更新新闻
	 * @param news
	 * @param dataStore
	 * @param userInfo
	 */
	@Override
	public void addOrEditNews(PortNews news, DataStorePort dataStore,
			IUserInfo userInfo) {
		
		//新增
		if(StringUtil.isEmpty(news.getNewsId())){
			addNewsDo(news, dataStore, userInfo);
		//更新
		}else{
			editNewsDo(news, dataStore, userInfo);
		}
	}

	/**
	 * 编辑新闻
	 * @param news
	 * @param dataStore
	 * @param userInfo
	 */
	private void editNewsDo(PortNews news, DataStorePort dataStore,
			IUserInfo userInfo) {
		
		//通过新闻id获取新闻信息
		PortNews newsHis = portNewsDao.getNewsById(news.getNewsId());
		if(newsHis == null){
			return;
		}
		
		//更新新闻
		newsHis.setAdvImg1(news.getAdvImg1());
		newsHis.setAdvImg2(news.getAdvImg2());
		newsHis.setAdvUrl1(news.getAdvUrl1());
		newsHis.setAdvUrl2(news.getAdvUrl2());
		newsHis.setContent(StringUtil.stringToClob(news.getContentStr()));
		newsHis.setDescription(news.getDescription());
		newsHis.setImage(news.getImage());
		newsHis.setShowTag(news.getShowTag());
		newsHis.setTag(news.getTag());
		newsHis.setTitle(news.getTitle());
		newsHis.setUpdateTime(new Date());
		newsHis.setUpdateUser(userInfo.getUserName());
		if(news.getIsrecom() == null){
			newsHis.setIsrecom(new BigDecimal(100));//推荐，默认100
		}else{
			newsHis.setIsrecom(new BigDecimal(1));
		}
		if(news.getUp() == null){
			newsHis.setUp(new BigDecimal(100));//置顶，默认100
		}else{
			newsHis.setUp(new BigDecimal(1));
		}
		if(news.getStick() == null){
			newsHis.setStick("0");//置顶，默认100
		}else{
			newsHis.setStick("1");
		}
		if(news.getTop() == null){
			newsHis.setTop(new BigDecimal(100));//头条，默认100
		}else{
			newsHis.setTop(new BigDecimal(1));
		}
		if(dataStore.getStatus() == null){
			newsHis.setNewsStatus("2");
		}else{
			newsHis.setNewsStatus(dataStore.getStatus());
		}
		//删除该新闻原有对应关系
		portNewsCateMapDao.delNewsCateMapByNewsId(news.getNewsId());
		//添加新闻分类对应关系
		if(!StringUtil.isEmpty(dataStore.getType())){
			List<PortNewsCateMap> list = new ArrayList<PortNewsCateMap>();
			
			String[] cateList = dataStore.getType().split(",");
			for (int i = 0; i < cateList.length; i++) {
				PortNewsCateMap item = new PortNewsCateMap();
				PortNewsCateMapId itemId = new PortNewsCateMapId();
				itemId.setNewsId(news.getNewsId());
				itemId.setCateId(cateList[i]);
				item.setId(itemId);
				
				list.add(item);
			}
			
			portNewsCateMapDao.addNewsCateMapList(list);
		}
		
	}

	/**
	 * 新增新闻
	 * @param news
	 * @param dataStore
	 * @param userInfo
	 */
	private void addNewsDo(PortNews news, DataStorePort dataStore,
			IUserInfo userInfo) {
		
		Date nowDate = new Date();
		
		news.setCollectNum(new BigDecimal(0));//收藏数
		news.setCommentCount(new BigDecimal(0));//评论数
		news.setCreateTime(nowDate);
		news.setCreateUser(userInfo.getUserName());
		news.setNewsStatus(PortConstant.NEWS_STATUS_SHOW);//状态，默认显示
		news.setPraiseNum(new BigDecimal(0));//点赞数
		news.setReadCount(new BigDecimal(0));//阅读数
		news.setShareNum(new BigDecimal(0));//分享数
		news.setUpdateTime(nowDate);
		news.setUpdateUser(userInfo.getUserName());
		news.setContent(StringUtil.stringToClob(news.getContentStr()));//新闻内容
		if(news.getIsrecom() == null){
			news.setIsrecom(new BigDecimal(100));//推荐，默认100
		}
		if(news.getUp() == null){
			news.setUp(new BigDecimal(100));//置顶，默认100
		}
		if(news.getTop() == null){
			news.setTop(new BigDecimal(100));//头条，默认100
		}
		
		//新增新闻
		news = portNewsDao.addNews(news);
		
		//新增新闻分类对应
		List<PortNewsCateMap> list = new ArrayList<PortNewsCateMap>();
		if(!StringUtil.isEmpty(dataStore.getType())){
			
			String[] cateList = dataStore.getType().split(",");
			for (int i = 0; i < cateList.length; i++) {
				PortNewsCateMap item = new PortNewsCateMap();
				PortNewsCateMapId itemId = new PortNewsCateMapId();
				itemId.setNewsId(news.getNewsId());
				itemId.setCateId(cateList[i]);
				item.setId(itemId);
				
				list.add(item);
			}
			
		//没选择分类，则属于主分类
		}else{
			PortNewsCateMap item = new PortNewsCateMap();
			PortNewsCateMapId itemId = new PortNewsCateMapId();
			itemId.setNewsId(news.getNewsId());
			itemId.setCateId(dataStore.getMainCateId());
			item.setId(itemId);
			
			list.add(item);
		}
		portNewsCateMapDao.addNewsCateMapList(list);
		
	}

	/**
	 * 通过新闻id获取新闻
	 * @param newsId
	 * @return
	 */
	@Override
	public PortNews getNewsById(String newsId) {
		
		PortNews news= portNewsDao.getNewsById(newsId);
		news.setContentStr(StringUtil.clobToString(news.getContent()));
		
		return news;
	}
	/**
	 * 获取所有带图片的新闻
	 */
	@Override
	public List<PortNews> getNewExistImage() {
		List<PortNews> list= portNewsDao.getNewExistImage();
		return list;
	}
	/**
	 * 获取置顶信息
	 */
	@Override
	public PortNews getTopNews() {
		PortNews news=portNewsDao.getTopNews();
		news.setContentStr(StringUtil.clobToString(news.getContent()));
		return news;
	}
	/**
	 * 获取最新新闻
	 */
	@Override
	public List<PortNews> getNewNews() {
		List<PortNews> list= portNewsDao.getNewNews();
		return list;
	}


	@Override
	public int updateNewShow(String ids, String isShow) {
		// TODO Auto-generated method stub
		return portNewsDao.updateNewShowDao(ids, isShow);
	}
	/**
	 *通过分类Id获取分类新闻
	 * @param cateId
	 * @param i
	 * @return
	 */
	@Override
	public List<PortNews> getCateNews(String cateId, int i) {
		List<PortNews> list= portNewsDao.getCateNews(cateId,i);
		return list;

	}
	/**
	 * 搜索新闻
	 */
	@Override
	public List<PortNews> getSearchNews(String key) {
		List<PortNews> list=portNewsDao.getSearchNews(key);
		return list;
	}
	/**
	 * 获取自分类的新闻
	 * @param cateId
	 * @param i
	 * @return
	 */
	@Override
	  public RetType getCateNews1(String cateId, int i) {
			
			RetType retType = new RetType();
			
			//获取所有
			List<PortNews> list = portNewsDao.getCateNews(cateId,i);
			List<PortNews> list2=new ArrayList<PortNews>();
			Iterator<PortNews> iterator = list.iterator(); 
		    while(iterator.hasNext()){  
		    	PortNews news=iterator.next();
		    	news.setContentStr(StringUtil.clobToString(news.getContent()));
		    	list2.add(news);
		    	
		    }
		    Iterator<PortNews> iterator1 = list2.iterator(); 
		    while(iterator1.hasNext()){  
		    	
		    	iterator1.next().setContent(null);
		    	
		    }
		    Iterator<PortNews> iterator3 = list2.iterator(); 
		    while(iterator3.hasNext()){  
		    	
		    	System.out.println(iterator3.next().getTitle());
		    	
		    }
			
			retType.doSuccess(list2);
			return retType;
		}

}
